﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nvn.Core.MVP;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.BusinessAccessLayer;
using Hutech.CRM.Library.Models.Entities;
using nvn.Library.Patterns;

namespace Hutech.CRM.Library.Presenters
{
    public class ListCriterionForGroupCustomerPresenter : BasePresenter<IListCriterionForGroupCustomerView>
    {
        #region First Description
        public ListCriterionForGroupCustomerPresenter(IListCriterionForGroupCustomerView view)
            : base(view)
        {
            this.view.FristLoadEventHandler += new EventHandler<EventArgs>(view_FristLoadEventHandler);
            //this.view.AddNew_EvenAgrs += new EventHandler<EventArgs>(view_AddNew_EvenAgrs);
            this.view.Reload_EventArgs += new EventHandler<EventArgs>(view_Reload_EventArgs);
            this.view.ListCriterionCustomer_ItemCommand+=new EventHandler<ItemCommandArgs<CriterionCustomer>>(view_ListCriterionCustomer_ItemCommand);
            this.view.ListCriterionCustomer_ItemDataBound += new EventHandler<ItemDataBoundArgs>(view_ListCriterionCustomer_ItemDataBound);
        }

        void view_Reload_EventArgs(object sender, EventArgs e)
        {
            view_FristLoadEventHandler(sender, e);
        }

        void view_ListCriterionCustomer_ItemCommand(object sender, ItemCommandArgs<CriterionCustomer> e)
        {
            Guid DataKeyValue = new Guid(e.parameterValue);

            CriterionCustomerBAL itemBAL = new CriterionCustomerBAL();
            CriterionCustomer item = itemBAL.GetOne(DataKeyValue);
            switch (e.parameterName)
            {
                case "EditCommand":
                    view.EditItemIndex = e.itemIndex.Value;
                    view.CriterionCustomers = itemBAL.GetAll();
                    break;

                case "ApplyCommand":
                    item.CriterionName = e.myType.CriterionName;
                    item.GroupLevelCustomerGuid = e.myType.GroupLevelCustomerGuid;
                    item.GroupLevelCustomerName = e.myType.GroupLevelCustomerName;
                    item.IsActive = e.myType.IsActive;
                    item.LastUpdatedDate = DateTime.Now;

                    if (itemBAL.Save(item))
                    {
                        view.EditItemIndex = -1;
                        view.CriterionCustomers = itemBAL.GetAll();
                    }
                    else 
                    { 
                    }
                    break;

                case "DeleteCommand":
                    itemBAL.Delete(DataKeyValue);
                    view.CriterionCustomers = itemBAL.GetAll();
                    //chu y chua xoa cac then con
                    break;

                case "EnableCommand":
                    item.IsActive = e.myType.IsActive;
                    if (itemBAL.Save(item))
                        view.CriterionCustomers = itemBAL.GetAll();
                    break;

                case "CancelCommand":
                    view.EditItemIndex = -1;
                    view.CriterionCustomers = itemBAL.GetAll();
                    //WebUtils.SetupRedirect(this, Request.RawUrl);
                    break;
            }
        }
        #endregion

        #region All Event
        void view_FristLoadEventHandler(object sender, EventArgs e)
        {
            CriterionCustomerBAL item = new CriterionCustomerBAL();
            view.CriterionCustomers = item.GetAll();
        }
        void view_ListCriterionCustomer_ItemDataBound(object sender, ItemDataBoundArgs e)
        {
            CriterionCustomerBAL itemBAL = new CriterionCustomerBAL();
            switch (e.parameterName)
            {
                case "LoadGroupLevel":
                    view.GroupLevelCustomers = itemBAL.GetGroupLevelCustomers();
                    break;
            }
        }
        #endregion

    }
}
